package 插入排序;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author : ZhuQi
 * @version : 时间:2022/8/11 11:40 版本:
 */
public class Demo {
    private static int[] array;

    /**
     * 初始化 数组
     */
    @BeforeEach
    public void beforeAll() {
        array = new int[]{1, 3, 5, 7, 9, 10, 8, 6, 4, 2, 0};
    }

    /**
     * 测试内容: 插入排序
     */
    @Test
    public void test01() throws Exception {
        for (int i = 1; i < array.length; i++) {
            int insertValue = array[i];
            int index = i - 1;
            while (index >= 0 && array[index] > insertValue) {
                array[index + 1] = array[index];
                index--;
            }
            array[index + 1] = insertValue;
        }
        System.out.println(Arrays.toString(array));
    }


    /**
     * 测试内容:
     * 测试结果:
     */
    @Test
    public void test02() throws Exception {
        for (int i = 1; i < array.length; i++) {
            int value = array[i];
            int index = i - 1;
            while (index >= 0 && array[index] > value) {
                array[index + 1] = array[index];
                index--;
            }
            array[index + 1] = value;
        }
        System.out.println(Arrays.toString(array));
    }


}
